import request from '@/utils/request'
import {encrypt,decrypt} from "@/utils/jsencrypt.js";

// 登录方法
export function login(username, password, code, uuid) {
  const data = {
    username,
    ciphertext: encrypt(password),	//密码密文
  }
	
	console.log(data)
  return request({
    url: '/app/login',
    method: 'post',
    data: data
  })
}

//通用登录
export function commonLogin(mode,param) {
	let fallMsg = {
				code: 1,
				msg: '失败'
			};
	
	return new Promise(resolve => {
		const loginFunc = {
			'api': login,
			'alipay': alipayLogin,
			'weixin': weixinLogin,
			'phone': phoneLogin
		}

		// 登录方式
		if(mode){
			try{
				console.log(mode)
				//远程登录
				switch(mode){
					case 'api':
						loginFunc[mode](param.username, param.password, param.code, param.uuid).then(res=>{
							console.log(res)
							resolve(res);
						});
						break;
					case 'alipay':
						loginFunc[mode](param.appid, param.code, param.scene, param.pid).then(res=>{
							console.log(res)
							resolve(res);
						});
						break;
					case 'weixin':
						loginFunc[mode](param.appid, param.code, param.scene, param.pid, param.type).then(res=>{
							console.log(res)
							resolve(res);
						});
						break;
					case 'phone':
						console.log(param)
						loginFunc[mode](param.phone,param.openid).then(res=>{
							console.log(res)
							resolve(res);
						});
						break;
				}		
				
			}catch(e){
				console.error(e);
			}
		}else{
			resolve(fallMsg)
		}
	});
}

//支付宝登录
function alipayLogin(appid,code,scene,pid){
	const data = {
	  appid,code,scene,pid
	}
	var param = '';
	for(var k in data){
		if(data[k]){
			param += '&'+k+'='+data[k];
		}
	}
	return request({
	  url: '/app/wx/login/alipay?'+param,
	  method: 'post',
	})
}

//微信登录
function weixinLogin(appid,code,scene,pid,type){
	const data = {
	  appid,code,scene,pid,type
	}
	var param = '';
	
	for(var k in data){
		if(data[k]){
			param += '&'+k+'='+data[k];
		}
	}
	console.log(param)
	return request({
	  url: '/app/wx/login?'+param,
	  method: 'post',
	})
}

//手机号登录
function phoneLogin(phone,openid){
	console.log(phone)
	return request({
	  url: '/app/wx/login/phone',
	  method: 'post',
		data: {
			phone: phone,
			openid: openid
		}
	})
}

// 获取用户详细信息
export function getInfo() {
  return request({
    url: '/getInfo',
    method: 'get'
  })
}

// 退出方法
export function logout() {
  return request({
    url: '/logout',
    method: 'post'
  })
}

// 获取验证码
export function getCodeImg() {
  return request({
    url: '/captchaImage',
    method: 'get'
  })
}

export function weixinCrypt(data){
	return request({
		url: "/app/wx/user/weixin/phoneNumber",
		method: "post",
		data: data
	})
}